$(document).ready(function() {
    //responsive menu toggle
    $("#menutoggle").click(function() {
        $('.xs-menu').toggleClass('displaynone');

    });
    //add active class on menu
    $('ul li').click(function(e) {
        e.preventDefault();
        $('li').removeClass('active');
        $(this).addClass('active');
    });
    //drop down menu
    $(".drop-down").hover(function() {
        $('.mega-menu').addClass('display-on');
    });
    $(".drop-down").mouseleave(function() {
        $('.mega-menu').removeClass('display-on');
    });

});




function closeCurrentWindow() {
    // 关闭当前浏览器窗口
    window.close();
}

// 页面加载时获取用户数据
window.onload = function () {
    fetchUserData();
};

function fetchUserData() {
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let userData = JSON.parse(xhr.responseText);
            updateForm(userData);
        }
    };

    // 替换以下URL为你的后端控制器的地址
    xhr.open("GET", "/getUserData", true);
    xhr.send();
}

function updateForm(userData) {
    document.getElementById("username").value = userData.username;
    document.getElementById("password").value = userData.password;
    document.getElementById("email").value = userData.email;
    document.getElementById("banStatus").value = userData.banStatus

    document.getElementById("username").disabled = true;
}

function submitModifiedData() {
    let formData = new FormData(document.getElementById("userInfoForm"));
    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            // 处理后端返回的响应
            console.log(xhr.responseText);
        }
    };

    // 替换以下URL为你的后端控制器的地址
    xhr.open("POST", "/updateUserData", true);
    xhr.send(formData);
}

function submitAppeal() {
    let username = document.getElementById("username").value;
    let email = document.getElementById("email").value;

    let xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            // 处理后端返回的响应
            console.log(xhr.responseText);
        }
    };

    // 替换以下URL为你的后端控制器的地址
    let url = "/submitAppeal";
    let data = "username=" + encodeURIComponent(username) + "&email=" + encodeURIComponent(email);
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send(data);
}
